package com.coffeeloversupportlibrary.core.aliyun.upload;
import android.util.Log;
import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.coffeeloversupportlibrary.coffeelover.CoffeeLoverApplication;
import java.io.File;
import java.util.Date;

/**
 * @author puyv
 * @createTime 2021/11/27 10:27
 * @projectName CoffeeLoverSupportLibrary
 * @className UpLoadFile.java
 * @description TODO
 */
public class UpLoadFile {
    //mercuryimg
    //endpoint
    private static final String ENDPOINT = "oss-cn-beijing.aliyuncs.com";
    //存储空间
    private static final String BUCKET_NAME = "mercuryimg";

    /**
    * @date 2021/11/27 10:31
    * @params []
    * @return com.alibaba.sdk.android.oss.OSS     OSSCredentialProvider
    * @description TODO 移动端为不被信任的环境 不建议使用该 凭证提供器  为client 对象提供凭证
     *  OSSCredentialProvider--- 凭证提供器的顶级接口 实现类能为 OSSClient 提供凭证
     *  如果使用 STS 安全令牌服务 使用  OSSStsTokenCredentialProvider  令牌凭证提供类
     *
    */
    private static OSS getOSSClient() {
        OSSCredentialProvider credentialProvider =
                new OSSPlainTextAKSKCredentialProvider("LTAI5tNgkcpfK7wNL7MXgkvG" ,
                        "iocrZUFJv2Nq7wbhvFvJ6WgusRAcSB");
        return new OSSClient(CoffeeLoverApplication.get(), ENDPOINT, credentialProvider);
    }

    public static void asyUpLoad(File file,OSSClient client,OATCallback callback){
        String objectKey="shareImg/"+file.getName();
        // 上传文件对象
        //p1 存储空间  p2 存储对象 全路径  p3 本地文件路径   shareImg/ 加文件名
        PutObjectRequest put = new PutObjectRequest(BUCKET_NAME, objectKey, file.getAbsolutePath());
        //进度回调
        put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>(){
            @Override
            public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
//                Log.e("OOS", "currentSize: " + currentSize + " totalSize: " + totalSize);
            }
        });

        OSSAsyncTask task =client.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
            @Override
            public void onSuccess(PutObjectRequest request, PutObjectResult result) {
                 callback.success();
            }

            @Override
            public void onFailure(PutObjectRequest request, ClientException clientException, ServiceException serviceException) {
                    callback.failure();
                    if(clientException != null){
                       clientException.printStackTrace();
                    }
                    if(serviceException != null){
                        Log.e("OOS", serviceException.getRawMessage());
                    }
            }
        });
        task.waitUntilFinished();
    }
    public interface OATCallback{
        public void success();
        public void failure();
    }
}
